package tk.pptiger.controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet(name="BodyView", urlPatterns={"/body.view"})
public class BodyViewServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest req,
                                  HttpServletResponse resp) throws ServletException, IOException {
        // 主要演示了怎么获取 http 请求的 body
        // 但如果是上传文件的话
        BufferedReader reader = req.getReader();
        String input = null;
        String requestBody = "";
        while((input = reader.readLine()) != null) {
            requestBody = requestBody + input + "<br>";
        }
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet BodyView</title>");
        out.println("</head>");
        out.println("<body>");
        out.println(requestBody);
        out.println("</body>");
        out.println("</html>");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
    }
}
